package name.fowler.refactoring.simplestore;

public class Reporter {
	private Customer customer;

	public Reporter(Customer customer) {
		this.customer = customer;
	}
	
	public String calculatedStatement()
	{
		double totalAmount = 0;
		int freqPoint = 0;
				
		String result = "Rental record for " + customer.getName() + "\n";
		for(Rental rental: customer.getRentals()) {
			result += "\t" + rental.getMovie().getTitle() + "\t" + calculateRentalAmount(rental) + "\n";
			freqPoint += calculateRentalFreqPoint(rental);
			totalAmount += calculateRentalAmount(rental);
		}

		result += createCommonRentalResult(totalAmount, freqPoint, result);
		return result;
	}

	public String createCommonRentalResult(double totalAmount, int freqPoint,
			String result) {
		return new StringBuffer()
				.append("Amount owed is " + totalAmount + "\n")
				.append("You earned " + freqPoint + " frequent renter points.")
			.toString();
	}

	public double calculateRentalAmount(final Rental rental) {
		switch(rental.getMovie().getPriceCode()) {
			case Regular:
				return calculateRegularMovieAmount(rental);
			case NewRelease:
				return calculateNewReleaseMovie(rental);
			case Childrens:
				return calculateChildrenMovie(rental);
			default:
				return 0;
		}
	}

	public double calculateChildrenMovie(final Rental rental) {
		return rental.getDaysRented() > 3 ? 
				((rental.getDaysRented() - 3) * 1.5) : 1.5;
	}

	public double calculateNewReleaseMovie(final Rental rental) {
		return rental.getDaysRented() * 3;
	}

	public double calculateRegularMovieAmount(Rental rental) {
		return rental.getDaysRented() > 2 ?
				((rental.getDaysRented() - 2) * 1.5 + 2) : 2;
	}
	
	public int calculateRentalFreqPoint(Rental rental){
		boolean isNewRelease = rental.getMovie().getPriceCode() == PriceCodes.NewRelease;
		boolean isRentedMoreOneDay = rental.getDaysRented() > 1;
		int rentalFreqPoint = isNewRelease && isRentedMoreOneDay ? 2 : 1;
		//TODO ветвление в зависимости от Customer (пересчет RentalFreqPoint)
		return rentalFreqPoint;
	}
}